package controller.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import db.DAOFactory;

import model.User;
import model.relation.UserGroup;

public class UserManager extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public UserManager() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doDelete method of the servlet. <br>
	 *
	 * This method is called when a HTTP delete request is received.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doDelete(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		response.setContentType("text/html");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		String msg="";
		
		//获取请求要获取的用户信息的ID，根据ID来返回信息
		String uri=request.getRequestURI();
		String[] uriStr=uri.split("/");
        int userId=Integer.parseInt(uriStr[uriStr.length-1]);

        if(DAOFactory.getUserDAO().delete(userId)){
        	msg="删除成功";
        	request.getSession().setAttribute("activeUser",null);
        }else
        	msg="删除失败";
		
		out.println("<p>"+msg+"</p>");
		out.flush();
		out.close();
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		//输出资源类型列表
		tools.OutputXml.output(new model.dto.user.UserList(), response);
	}

	/**
	 * The doPut method of the servlet. <br>
	 *
	 * This method is called when a HTTP put request is received.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPut(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		System.out.println("receive put request.");
		response.setContentType("text/html");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		
		//获取请求要获取的用户信息的ID，根据ID来返回信息
		String uri=request.getRequestURI();
		String[] uriStr=uri.split("/");
        int userId=Integer.parseInt(uriStr[uriStr.length-1]);
        
		//接受来自客户端请求中的数据
		Scanner s = new Scanner(request.getInputStream());
		String data=null;
		if(s.hasNextLine())
			data=s.nextLine();
		if(data!=null){
			Integer roleId=Integer.parseInt(data);	
			if(DAOFactory.getUserGroupDAO().findByUserId(userId)==null)
				if(DAOFactory.getUserGroupDAO().save(new UserGroup(DAOFactory.getRoleDAO().findById(roleId),userId)))
					out.println("修改成功.");
				else
					out.println("修改失败!");
			else if(DAOFactory.getUserGroupDAO().update(new UserGroup(DAOFactory.getRoleDAO().findById(roleId),userId)))
				out.println("修改成功.");				
		}else
			out.println("修改失败!");
		out.flush();
		out.close();
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
